package com.updesignsz.admin.controller;

import com.updesignsz.admin.service.FunctionService;
import com.updesignsz.admin.util.ResponseUtil;
import com.updesignsz.admin.vo.form.FunctionForm;
import com.updesignsz.commom.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * TODO
 * Created User: lx
 * Date: 2020/4/29 15:39
 */
@RestController
public class FunctionController {

    @Autowired
    private FunctionService functionService;

    @GetMapping("/function")
    public void function(HttpServletResponse response) {
        ResultVo vo = new ResultVo();
        vo.setData(functionService.listAll());
        ResponseUtil.outJson(vo.toJsonMsg(), response);
    }


    @PostMapping("/function")
    public void addfunction(@Validated(PostMapping.class) @RequestBody FunctionForm form, HttpServletResponse response) {
        ResultVo vo = new ResultVo();
        functionService.saveFunction(form);
        vo.setData(functionService.getById(form.getId()));
        ResponseUtil.outJson(vo.toJsonMsg(), response);
    }


    @DeleteMapping("/function/{functionId}")
    public void deletefunction(@PathVariable Integer functionId, HttpServletResponse response) {
        ResultVo vo = new ResultVo();
        functionService.deleteFunction(functionId);
        ResponseUtil.outJson(vo.toJsonMsg(), response);
    }

    @PutMapping("/function")
    public void updatefunction(@Validated(PutMapping.class) @RequestBody FunctionForm form, HttpServletResponse response) {
        ResultVo vo = new ResultVo();
        functionService.updateFunction(form);
        vo.setData(functionService.getById(form.getId()));
        ResponseUtil.outJson(vo.toJsonMsg(), response);
    }
}
